

import cv2
import os

def read_path(file_pathname):
    #遍历该目录下的所有图片文件
    for filename in os.listdir(file_pathname):
        print(filename)
        img = cv2.imread(os.path.join(file_pathname,filename))

# 仅允许英文目录，可以新建文件夹
def save_img(dest_path, img):
    os.makedirs(os.path.dirname(dest_path), exist_ok=True)
    cv2.imwrite(dest_path, img)
        
# 这个函数用来将三个文件夹中的中文改为拼音，并且在新的图片文件名后加后缀
def gen_path(origin, suffix, folder_suffix=None):
    path1, fextension = os.path.splitext(origin)
    path2, img_name = os.path.split(path1)
    path3, classification = os.path.split(path2)
    if classification == "受损+紧经":
        classification = "shousun+jinjing"
    elif classification == "油污+缺纬":
        classification = "youwu+quewei"
    elif classification == "麻皮":
        classification = "mapi"
    classification = classification + folder_suffix
    img_name = img_name + suffix + ".bmp"
    return os.path.join(path3, classification, img_name)

# 用于生成新的分类文件夹路径, 以及新的图片文件名
def gen_new_path(origin, new_class, new_img_name):
    path1, fextension = os.path.splitext(origin)
    path2, img_name = os.path.split(path1)
    path3, classification = os.path.split(path2)
    new_img_name = new_img_name + fextension
    return os.path.join(path3, new_class, new_img_name)
    
    


# def save_all_equ_histo(src_folder_path, dest_folder_path):
#     for filename in os.listdir(src_folder_path):
#         print(filename)
#         img = cv2.imread(src_folder_path+'/'+filename)
    


if __name__ == "__main__":
    read_path("/home/hugoxana/Documents/fabric-defect-detection/fabric-defect/受损+紧经")
    path = "../fabric-defect/受损+紧经/T03869_00.bmp"
    print(gen_path(path, "_equhisto","_equhisto"))
    print(gen_new_path(path,"crop_normal","crop_normal_1"))